#include "hd.h"
#include <avr/eeprom.h>
#include "status.h"

//*****************************************
//
//             eeprom
//
//*****************************************
uint8_t eepromLength=0x04;//
uint16_t eepromAddress[]={0x00,0x00,0x00,0x00};
uint16_t *eepromVar[]={0,0,0,0};
uint8_t eepromS[]={0x00,0x00,0x00,0x00};
// 32 01 read 10 write 1 0 01 byte 10 word
uint8_t eepromHead=0x00;
uint8_t eepromTail=0x00;
uint8_t eepromFull=0x00;// 01 full 10 empty4


uint8_t eepromGet(void)//查询有多少个空位
{
    if(eepromHead>=eepromTail)
    {
        return (eepromLength-(eepromHead-eepromTail));
    }
    else
    {
        return(eepromTail-eepromHead);
    }
}

void eepromSet(uint16_t eeA,uint16_t *eeV,uint8_t eeS)//注意第二个参数传递的是地址
{
    eepromAddress[eepromHead]=eeA;
    eepromVar[eepromHead]=eeV;
    eepromS[eepromHead]=eeS;
    eepromHead++;
    if(eepromHead==eepromLength)
    {
        eepromHead=0x00;
    }
}

void eepromReadAndWrite(void)//
{
    uint8_t ees=0x00;
    uint8_t i_eeraw=0x00;
    for(;i_eeraw<3;i_eeraw++)
    {
        if(eepromHead==eepromTail)
        {
            return;
        }
        ees=eepromS[eepromTail];
        if(ees&0x0f)
        {
            cli();
            if((ees&0x0c)==0x04)
            {
                if((ees&0x03)==0x01)
                {
                    *eepromVar[eepromTail]=eeprom_read_byte((void * )eepromAddress[eepromTail]);
                }
                else if((ees&0x03)==0x02)
                {
                    *eepromVar[eepromTail]=eeprom_read_word((void * )eepromAddress[eepromTail]);
                }
            }
            else if((ees&0x0c)==0x08)
            {
                if((ees&0x03)==0x01)
                {
                    eeprom_write_byte((void *)eepromAddress[eepromTail], *eepromVar[eepromTail]);
                }
                else if((ees&0x03)==0x02)
                {
                    eeprom_write_word((void *)eepromAddress[eepromTail], *eepromVar[eepromTail]);
                }
            }
            eepromS[eepromTail]&=0xf0;
            eepromTail++;
            if(eepromTail==eepromLength)
            {
                eepromTail=0x00;
            }
            sei();
        }
    }
}

//*********************************************
//
//             倒数子函数
//
//*********************************************

uint8_t countDown(uint16_t value0seOrGas,uint8_t zeroOrGas)//zeroOrGas 0 zero 1 gas
{
    static uint8_t timeCount20ms=20;
    if(bf)
    {
        timeCount20ms--;
    }
    if(timeCount20ms<4)
    {
        value.valueSum+=value0seOrGas;
    }
	if(timeCount20ms==00)
	{
		timeCount20ms=20;
		return 1;
	}
	if(ControlS&0x01)
	{
		if(zeroOrGas)
		{
		    numberTurn(value0seOrGas,1);
		}
		else
		{
		    numberTurn(value0seOrGas,0);
		}
	}
	else
	{
	    numberTurn(timeCount20ms,0);
	}
	if(bf)
	{
	    zero_on;
	}
	bf^=0x01;
	return 0;
}

//***************************************************
//
//                 gas
//
//***************************************************
void gas(void)
{
	uint16_t g=0x0000;
	unsigned int g_o2=0x00;
	switch(value.dir)
	{
		case 0:
			if(value.g_wADCValue>value.valueZero)
			{
			    g=value.g_wADCValue-value.valueZero;
			}
			else
			{
			    g=0;
			}
//			g=value.g_wADCValue-value.value_zero;
			if(g>threshold) value.valueGas=(unsigned int)(((unsigned long)g*(unsigned long)value.valueMultiple)>>7);
			else value.valueGas=0x0000;
		break;
		case 1:
			if(value.valueZero>value.g_wADCValue)
			{
			    g=value.valueZero-value.g_wADCValue;
			}
			else
			{
			    g=0;
			}
//			g=value.value_zero-value.g_wADCValue;
			if(g>threshold) value.valueGas=(unsigned int)(((unsigned long)g*(unsigned long)value.valueMultiple)>>7);
			else value.valueGas=0x0000;
		break;
		case 2://O2的multiple 表示浓度0时候的AD数值
			if((value.g_wADCValue+threshold)<value.valueZero)
			{
				g_o2=(unsigned int)(((unsigned long)(value.valueZero-value.g_wADCValue)*(unsigned long)value.valueMultiple)>>7);
				if(g_o2>=gas0) g_o2=gas0;
				value.valueGas=gas0-g_o2;
			}
			else if(value.g_wADCValue>(threshold+value.valueZero))
			{
			    value.valueGas=gas0+(unsigned int)(((unsigned long)(value.g_wADCValue-value.valueZero)*(unsigned long)value.valueMultiple)>>7);
			}
			else value.valueGas=gas0; //209
		break;
	}
}

//*********************************************
//
//              high-low
//
//*********************************************
uint8_t lowHigh(uint16_t (* value_p),uint8_t low_high) // f_dir 0 address 1 low 2 high
{
//	if((key_short)||(key_cont)) cs_add=disNew-1;
	if((key_short==0b00000100)||(key_cont==0b00000100))
	{
		if(low_high)
		{
			if(value.dir==0x02)
			{
				if((* value_p)<value.valueLow) (* value_p)++;
			}
			else if((value.dir==0x00)||(value.dir==0x01))
			{
				if((* value_p)<a0) (* value_p)++;
			}
		}
		else
		{
			if(value.dir==0x02)
			{
				if((* value_p)<a0) (* value_p)++;
			}
			else if((value.dir==0x00)||(value.dir==0x01))
			{
				if((* value_p)<value.valueHigh) (* value_p)++;
			}
		}
	}
	if((key_short==0b00000010)||(key_cont==0b00000010))
	{
		if(low_high)
		{
			if(value.dir==0x02)
			{
				if((* value_p)>0) (* value_p)--;
			}
			else if((value.dir==0x00)||(value.dir==0x01))
			{
				if((* value_p)>value.valueLow) (* value_p)--;
			}
		}
		else
		{
			if(value.dir==0x02)
			{
				if((* value_p)>value.valueHigh) (* value_p)--;
			}
			else if((value.dir==0x00)||(value.dir==0x01))
			{
				if((* value_p)>0) (* value_p)--;
			}
		}
	}
	if(key_short==0b00000001)
	{
		if(low_high==1)
		{
		    eepromSet(eeValueHighWord0,value_p,eeWriteWord);
		    if(eeStatus0&0x0002)
		    {
		        eeStatus0&=0xfffd;
		        eepromSet(eeStatusWord0,&eeStatus0,eeWriteWord);
		    }
		}
		else
		{
		    eepromSet(eeValueLowWord0,value_p,eeWriteWord);
		    if(eeStatus0&0x0001)
		    {
		        eeStatus0&=0xfffe;
		        eepromSet(eeStatusWord0,&eeStatus0,eeWriteWord);
		    }
		}
		return 1;
	}
	if(bf)
	{
	    numberTurn(*value_p,pointS);
	}
	low_on;
	bf^=0x01;
	return 0;
}

//**********************************************
//
//                current
//
//**********************************************
uint8_t fun_ma(uint16_t (* value_ma),uint8_t f_ma)
{
	uint8_t i_fma=0x00;
	if((key_short)||(key_cont)) cs_add=disNew-1;
	if((key_short==0b00000100)||(key_cont==0b00000100)) (* value_ma)++;
	if((key_short==0b00000010)||(key_cont==0b00000010)) (* value_ma)--;
	switch(f_ma)
	{
		case 0x00:
			if((* value_ma)==0) (* value_ma)=1;
			if((* value_ma)==0x03ff) (* value_ma)=0x03fe;
		break;
		case 0x01: //4ma
			if((* value_ma)==value.valueCurrent8ma) (* value_ma)=value.valueCurrent8ma-1;
			if((* value_ma)==0) (* value_ma)=1;

		break;
		case 0x02: //8
			if((* value_ma)==value.valueCurrent12ma) (* value_ma)=value.valueCurrent12ma-1;
			if((* value_ma)==value.valueCurrent4ma) (* value_ma)=value.valueCurrent4ma+1;
		break;
		case 0x03: //12
			if((* value_ma)==value.valueCurrent16ma) (* value_ma)=value.valueCurrent16ma-1;
			if((* value_ma)==value.valueCurrent8ma) (* value_ma)=value.valueCurrent8ma+1;
		break;
		case 0x04: //16
			if((* value_ma)==value.valueCurrent20ma) (* value_ma)=value.valueCurrent20ma-1;
			if((* value_ma)==value.valueCurrent12ma) (* value_ma)=value.valueCurrent12ma+1;
		break;
		case 0x05: //20
			if((* value_ma)==0x03ff) (* value_ma)=0x03fe;
			if((* value_ma)==value.valueCurrent16ma) (* value_ma)=value.valueCurrent16ma+1;
		break;
	}
	if(key_short==0b00000001)
	{
		eepromSet((0x32+2*f_ma),value_ma,eeWriteWord);
		for(i_fma=0;i_fma<6;i_fma++)
		{
			if(i_fma==f_ma)
			{
				if(eeStatus0&(1<<(i_fma+2)))
				{
					eeStatus0&=(~(1<<(i_fma+2)));
					eepromSet(eeStatusWord0,&eeStatus0,eeWriteWord);
				}
			}
		}
		return 1;
	}
//	if((key_short)||(key_long)) bf=0x01;
//	else bf^=0x01;
    if(bf)
	{
	    numberTurn(*value_ma,0);
	}
	bf^=0x01;
	return 0;
}











